//
//  UserModel.m
//
//  Created by ios on 2016/11/1.
//  Copyright © 2016年 appleLJ. All rights reserved.
//

#import "UserModel.h"
static NSString * const KEY_USERID = @"KEY_USERID";
static NSString * const KEY_USER_MOBILE = @"LOGISTICS_USER_MOBILE";
static NSString * const KEY_OPENID = @"KEY_OPENID";
static NSString * const KEY_NICKNAME = @"KEY_NICKNAME";
static NSString * const KEY_USERPocketID = @"KEY_USERPocketID";
static NSString * const KEY_IMGURL = @"KEY_IMGURL";
static NSString * const KEY_TOKEN = @"KEY_TOKEN";
static NSString * const KEY_BAIJIN = @"KEY_BAIJIN";

@interface UserModel();

@end

@implementation UserModel

+(UserModel*)sharedInstanced{
    static UserModel *_userLogon = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _userLogon = [[UserModel alloc] init];
    });
    return _userLogon;
}

- (id)init{
    self = [super init];
    if (!self) {
        return nil;
    }
    return self;
}

#pragma mark - userID

- (NSString *)userID {
    //registerDefaults作用：如果KEY_USERID有值，那么这句话没用；如果无值的话，那么会存值为“”
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_USERID:@""}];
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_USERID];
}

- (void)setUserID:(NSString *)userID {
    if (!userID) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_USERID];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentUserID = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_USERID];
    //判断是否需要更新
    if ([currentUserID isEqualToString:userID]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:userID forKey:KEY_USERID];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 电话
 
 @param mobile 电话
 */
- (void)setMobile:(NSString *)mobile{
    if (!mobile) {
        [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:KEY_USER_MOBILE];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    NSString *preMobile= [[NSUserDefaults standardUserDefaults] valueForKey:KEY_USER_MOBILE];
    if (preMobile == mobile) {
        return;
    }
    
    [[NSUserDefaults standardUserDefaults] setValue:mobile forKey:KEY_USER_MOBILE];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)mobile{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_USER_MOBILE:@""}];
    return [[NSUserDefaults standardUserDefaults] valueForKey:KEY_USER_MOBILE];
}

/**
 @param openId 微信id
*/
- (void)setOpenId:(NSString *)openId{
    if (!openId) {
        [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:KEY_OPENID];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    NSString *preOpenId= [[NSUserDefaults standardUserDefaults] valueForKey:KEY_OPENID];
    if (preOpenId == openId) {
        return;
    }
    
    [[NSUserDefaults standardUserDefaults] setValue:openId forKey:KEY_OPENID];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)openId{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_OPENID:@""}];
    return [[NSUserDefaults standardUserDefaults] valueForKey:KEY_OPENID];
}

#pragma mark - nickName

- (NSString *)nickName{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_NICKNAME:@""}];
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_NICKNAME];
}

/**
 设置用户名
 
 @param nickName 用户名
 */
- (void)setNickName:(NSString *)nickName{
    if (!nickName) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_NICKNAME];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentNickName = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_NICKNAME];
    //判断是否需要更新
    if ([currentNickName isEqualToString:nickName]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:nickName forKey:KEY_NICKNAME];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
/**
 设置用户名
 
 @param learnTime 学习时间
 */
- (void)setLearnTime:(NSString *)learnTime{
    if (!learnTime) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_NICKNAME];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentNickName = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_NICKNAME];
    //判断是否需要更新
    if ([currentNickName isEqualToString:learnTime]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:learnTime forKey:KEY_NICKNAME];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

#pragma mark - imgUrl

- (NSString *)imgUrl{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_IMGURL:@""}];
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_IMGURL];
}

/**
 设置背景图片

 @param imgUrl 背景图片
 */
- (void)setImgUrl:(NSString *)imgUrl{
    if (!imgUrl) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_IMGURL];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentImgUrl = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_IMGURL];
    //判断是否需要更新
    if ([currentImgUrl isEqualToString:imgUrl]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:imgUrl forKey:KEY_IMGURL];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

#pragma mark - token

- (NSString *)token{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_TOKEN:@""}];
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_TOKEN];
}

/**
 token
 
 @param token token description
 */
- (void)setToken:(NSString *)token{
    if (!token) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_TOKEN];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentImgUrl = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_TOKEN];
    //判断是否需要更新
    if ([currentImgUrl isEqualToString:token]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:KEY_TOKEN];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 *  判断白金状态
 *
 *  @param baijin 白金会员 0不是 1是 默认0
 */
- (NSString *)baiJin{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{KEY_BAIJIN:@""}];
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BAIJIN];
}
- (void)setBaiJin:(NSString *)baiJin{
    if (!baiJin) {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_BAIJIN];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    //获取本地数据
    NSString *currentImgUrl = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BAIJIN];
    //判断是否需要更新
    if ([currentImgUrl isEqualToString:baiJin]) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:baiJin forKey:KEY_BAIJIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
